package org.example.forkjoin;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // test2(0L,10_0000_0000L);  // sum需要的时间 = 9428  7855          500000000500000000
        test3();
    }

    public static void test1(Long start, Long end ){
        Long sum = 0L;
        Long a = System.currentTimeMillis();
        for ( Long i= start; i < end; i++) {
            sum += i;
        }
        Long b = System.currentTimeMillis();
        System.out.println("sum需要的时间 = "+(b -a));
    }

    public static void test2(Long start, Long end ) throws ExecutionException, InterruptedException {
        Long a = System.currentTimeMillis();

        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask<Long> myForkJoin = new myForkJoin(start, end);

        ForkJoinTask<Long> submit = forkJoinPool.submit(myForkJoin);
        Long aLong = submit.get();


        Long b = System.currentTimeMillis();
        System.out.println("sum需要的时间 = "+aLong +"time:"+(b -a));
    }

    public static void test3(){

        Long a = System.currentTimeMillis();
        long reduce = LongStream.rangeClosed(0L, 10_0000_0000L).parallel().reduce(0L, Long::sum);
        Long b = System.currentTimeMillis();
        System.out.println("sum需要的时间 = "+(b -a) +"结果"+reduce);
    }
}
